package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbDiscountCondition;
import com.ruoyi.system.service.ITbDiscountConditionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 团购规则设置Controller
 *
 * @author ruoyi
 * @date 2021-02-19
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/condition" )
public class TbDiscountConditionController extends BaseController {

    private final ITbDiscountConditionService iTbDiscountConditionService;

    /**
     * 查询团购规则设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:condition:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbDiscountCondition tbDiscountCondition) {
        startPage();
        List<TbDiscountCondition> list = iTbDiscountConditionService.queryList(tbDiscountCondition);
        return getDataTable(list);
    }

    /**
     * 导出团购规则设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:condition:export')" )
    @Log(title = "团购规则设置" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(TbDiscountCondition tbDiscountCondition) {
        List<TbDiscountCondition> list = iTbDiscountConditionService.queryList(tbDiscountCondition);
        ExcelUtil<TbDiscountCondition> util = new ExcelUtil<TbDiscountCondition>(TbDiscountCondition.class);
        return util.exportExcel(list, "condition" );
    }

    /**
     * 获取团购规则设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:condition:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iTbDiscountConditionService.getById(id));
    }

    /**
     * 新增团购规则设置
     */
    @PreAuthorize("@ss.hasPermi('system:condition:add')" )
    @Log(title = "团购规则设置" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbDiscountCondition tbDiscountCondition) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        tbDiscountCondition.setUserId(userId + "");
        return toAjax(iTbDiscountConditionService.save(tbDiscountCondition) ? 1 : 0);
    }

    /**
     * 修改团购规则设置
     */
    @PreAuthorize("@ss.hasPermi('system:condition:edit')" )
    @Log(title = "团购规则设置" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbDiscountCondition tbDiscountCondition) {
        return toAjax(iTbDiscountConditionService.updateById(tbDiscountCondition) ? 1 : 0);
    }

    /**
     * 删除团购规则设置
     */
    @PreAuthorize("@ss.hasPermi('system:condition:remove')" )
    @Log(title = "团购规则设置" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(iTbDiscountConditionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
